﻿using IServices;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Linq;
using YL.Core.Dto;
using YL.Core.Entity;
using YL.Core.Entity.Fluent.Validation;
using YL.NetCore.Attributes;
using YL.NetCore.NetCoreApp;
using YL.Utils.Extensions;
using YL.Utils.Pub;
using YL.Utils.Table;

namespace Method.SmartMeter.Controllers
{
    //设备状态查看及设备当前实时数据页面
    public class DeviceDataController : BaseController
    {
        private readonly IMeter_DeviceDataServices _deviceDataServices;
        private readonly ISys_roleServices _roleServices;
        private readonly IMemoryCache _cache;
        private readonly IMediator _mediator;

        public DeviceDataController(IMeter_DeviceDataServices deviceDataServices,ISys_roleServices roleServices, 
            IMemoryCache cache, IMediator mediator)
        {
            _deviceDataServices = deviceDataServices;
            _roleServices = roleServices;
            _cache = cache;
            _mediator = mediator;
        }

        //设备状态查看
        public IActionResult Index()
        {
            ViewBag.keywords = GetDescriptor("keywords");
            ViewBag.description = GetDescriptor("description");
            ViewBag.company = GetDescriptor("company");
            ViewBag.customer = GetDescriptor("customer");
            ViewBag.nickname = UserDtoCache.UserNickname;
            ViewBag.UserName = UserDtoCache.UserName;
            ViewBag.UserId = UserDtoCache.UserId;//待完善
            ViewBag.headimg = UserDtoCache.HeadImg;

            //菜单
            var menus = _roleServices.GetMenu(UserDtoCache.RoleId.Value);
            GetMemoryCache.Set("menu_" + UserDtoCache.UserId, menus);
            ViewData["menu"] = menus;
            return View();
        }
        //设备状态查看
        [HttpPost]
        [OperationLog(LogType = LogType.select)]
        public ContentResult List([FromForm]Bootstrap.BootstrapParams bootstrap)
        {
            var sd = _deviceDataServices.PageList(bootstrap, UserDtoCache.UserId);
            return Content(sd);
        }

        //地图中标记数据
        [HttpPost]
        [OperationLog(LogType = LogType.select)]
        public ContentResult MapList([FromForm]Bootstrap.BootstrapParams bootstrap)
        {
            var sd = _deviceDataServices.MapList(bootstrap, UserDtoCache.UserId);
            return Content(sd);
        }

        //地图中标记数据
        [HttpGet]
        [OperationLog(LogType = LogType.select)]
        public ContentResult RoughMapList()
        {
            var sd = _deviceDataServices.RoughMapList(UserDtoCache.UserId);
            return Content(sd);
        }

        //设备当前实时数据
        [HttpGet]
        [CheckMenu]
        public IActionResult MachineDetails()
        {
            if (!string.IsNullOrEmpty(Request.Query["ClientId"]))
            {
                ViewBag.ClientId = Convert.ToInt64(Request.Query["ClientId"]);
            }
            return View();
        }
        //设备当前实时数据
        [HttpPost]
        [OperationLog(LogType = LogType.select)]
        public ContentResult MachineDetailsList([FromForm]Bootstrap.BootstrapParams bootstrap)
        {
            Int64 ClientId = Convert.ToInt64(Request.Query["ClientId"]);
            var sd = _deviceDataServices.MachineDetailsList(bootstrap, ClientId, UserDtoCache.UserId);
            return Content(sd);
        }

        //设备当前实时数据
        [HttpGet]
        [CheckMenu]
        public IActionResult MachineDetailsTrend()
        {
            if (!string.IsNullOrEmpty(Request.Query["ClientId"]))
            {
                ViewBag.ClientId = Convert.ToInt64(Request.Query["ClientId"]);
            }
            return View();
        }
        //设备当前实时数据
        [HttpPost]
        [OperationLog(LogType = LogType.select)]
        public ContentResult MachineDetailsTrendList([FromForm]Bootstrap.BootstrapParams bootstrap)
        {
            Int64 ClientId = Convert.ToInt64(Request.Query["ClientId"]);
            var sd = _deviceDataServices.MachineDetailsList(bootstrap, ClientId, UserDtoCache.UserId);
            return Content(sd);
        }

    }
}